; ==========
; This file doesn't make sense to run alone
; ==========
#Requires AutoHotkey v2.0
#SingleInstance Force

class Setting extends Gui {
  static ins := Setting()

  __New() {
    super.__New('+AlwaysOnTop')
    this.SetFont('s13', 'Microsoft YaHei UI')
    tab := this.AddTab3('w300', ["截图框", '遮罩', "贴图", "其他"])
    tab.UseTab(1)
    this.AddCheckbox('VT1C1', '使用透明色')
      .OnEvent('Click', (obj, *) => this.OnClick1(obj, this['T1E1'], this['T1E2']))
    this.AddText('Section', '截图背景色:')
    this.AddEdit('yp vT1E0 Background' config.guiBgc, A_Tab)
    this.AddEdit('yp vT1E1', config.guiBgc)
      .OnEvent('Change', (obj, *) => this.OnChange(obj, this['T1E0']))
    this.AddText('xs', '背景透明度:')
    this.AddEdit('yp vT1E2 w60', config.guiTransparent)
    tab.UseTab(2)
    this.AddCheckbox('vT2C1', '显示遮罩')
      .OnEvent('Click', (obj, *) => this.OnClick2(obj, this['T2E1'], this['T2E2']))
    this.AddText('Section vT2C', '遮罩背景色:')
    this.AddEdit('yp vT2E0 Background' config.maskBgc, A_Tab)
    this.AddEdit('yp vT2E1', config.maskBgc)
      .OnEvent('Change', (obj, *) => this.OnChange(obj, this['T2E0']))
    this.AddText('xs', '遮罩透明度:')
    this.AddEdit('yp vT2E2  w60', config.maskInitTransparent)
    tab.UseTab(3)
    this.AddCheckbox('vT3C1', '立即贴图').OnEvent('Click', (*) => config.pasteInstantly := this['T3C1'].Value)
    this.AddCheckbox('vT3C2', '限定单张贴图').OnEvent('Click', (*) => config.singleton := this['T3C2'].Value)
    this.AddCheckbox('vT3C3', '贴图后保存为历史').OnEvent('Click', (*) => config.autoSave := this['T3C3'].Value)

    this.OnEvent('Close', (*) => this.OnDestory())
    tab.UseTab(4)
    this.AddCheckbox('vT4C1', '截图时显示更多信息')
      .OnEvent('Click', (obj, *) => config.withTip := obj.Value)
    this.Init()
  }

  Diff() {
    loop this.initState.Length {
      if this.initState[A_Index] != this.endState[A_Index] {
        return true
      }
    }
    return false
  }

  OnChange(obj, target) {
    if StrLen(obj.Text) = 6 {
      target.Opt('+Background' obj.Text)
      obj.Opt('cblack')
    }
    else {
      obj.Opt('cRed')
    }
  }

  static Show(*) {
    Setting.ins.Show()
  }

  Init() {
    if config.noBgc {
      this['T1C1'].Value := 1
      this['T1E1'].Opt('+ReadOnly cRed')
      this['T1E2'].Opt('+ReadOnly cRed')
    }
    if config.showMask {
      this['T2C1'].Value := 1
    }
    else {
      this['T2E1'].Opt('+ReadOnly cRed')
      this['T2E2'].Opt('+ReadOnly cRed')
    }
    this['T3C1'].Value := config.pasteInstantly
    this['T3C2'].Value := config.singleton
    this['T4C1'].Value := config.withTip
    this.initState := Array(config.noBgc, config.guiBgc, config.guiTransparent
      , config.showMask, config.maskBgc, config.maskInitTransparent
      , config.pasteInstantly, config.singleton, config.autoSave
      , config.withTip)
  }

  OnClick1(obj, t1, t2) {
    if obj.Value {
      t1.Opt('+ReadOnly cRed')
      t2.Opt('+ReadOnly cRed')
      config.noBgc := true
    } else {
      t1.Opt('-ReadOnly cBlack')
      t2.Opt('-ReadOnly cBlack')
      config.noBgc := false
    }
  }

  OnClick2(obj, t1, t2) {
    if obj.Value {
      t1.Opt('-ReadOnly cBlack')
      t2.Opt('-ReadOnly cBlack')
      config.showMask := true
    } else {
      t1.Opt('+ReadOnly cRed')
      t2.Opt('+ReadOnly cRed')
      config.showMask := false
    }
  }

  OnDestory(*) {
    this.endState := Array(this['T1C1'].Value, this['T1E1'].Text, this['T1E2'].Text
      , this['T2C1'].Value, this['T2E1'].Text, this['T2E2'].Text
      , this['T3C1'].Value, this['T3C2'].Value, this['T3C3'].Value
      , this['T4C1'].Value
    )
    if !this.Diff()
      return
    cfs.Set('noBgc', this['T1C1'].Value)
    if StrLen(this['T1E1'].Text) = 6 {
      cfs.Set('guiBgc', this['T1E1'].Text)
      config.guiBgc := this['T1E1'].Text
    }
    if this['T1E2'].Text > 0 && this['T1E2'].Text <= 255 {
      cfs.Set('guiTRP', this['T1E2'].Text)
      config.guiTransparent := this['T1E2'].Text
    }
    cfs.Set('showMask', this['T2C1'].Value)
    if StrLen(this['T2E1'].Text) = 6 {
      cfs.Set('maskBgc', this['T2E1'].Text)
      config.maskBgc := this['T2E1'].Text
    }
    if this['T2E2'].Text > 0 && this['T2E2'].Text <= 255 {
      cfs.Set('maskTRP', this['T2E2'].Text)
      config.maskInitTransparent := this['T2E2'].Text
    }
    cfs.Set('pasteIST', this['T3C1'].Value)
    cfs.Set('singleton', this['T3C2'].Value)
    cfs.Set('autoSave', this['T3C3'].Value)
    cfs.Set('tip', this['T4C1'].Value)
    cfs.Update()
  }
}